package com.zzptc.lzh.myapp20;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.zzptc.lzh.myapp20.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    EditText yhm_lzh,mm_lzh;
    Switch zddl_lzh;
    Button dl_lzh;
    boolean flag=false;
    List<User>list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        event();
        //拟造数据
        User user=new User("hhh","123");
        User user2=new User("kkk","456");
        list.add(user);
        list.add(user2);
    }

    private void event() {
        zddl_lzh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    flag=true;
                }else{
                    flag=false;
                    Toast.makeText(MainActivity.this, ""+isChecked, Toast.LENGTH_SHORT).show();
                }
            }
        });
        dl_lzh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = yhm_lzh.getText().toString().trim();
                String pws = mm_lzh.getText().toString().trim();
                SharedPreferences sh = getSharedPreferences("one", MODE_PRIVATE);
                SharedPreferences.Editor edit = sh.edit();
                if (flag){
                    edit.putString("username",name);
                    edit.putString("password",pws);
                    edit.putBoolean("autologin",flag);
                    edit.commit();
                }else{
                    edit.putString("username","");
                    edit.putString("password","");
                    edit.putBoolean("autologin",false);
                    edit.commit();
                }
                login(name,pws);
            }
        });
    }
    public void login(String username,String password){
        boolean islogin=false;
        for (User u:list){
            if (u.getUsername().equals(username)&&u.getPassword().equals(password)){
                islogin=true;
                break;
            }else{
                islogin=false;
            }
        }
        if (islogin){
            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        yhm_lzh=findViewById(R.id.yhm_lzh);
        mm_lzh=findViewById(R.id.mm_lzh);
        zddl_lzh=findViewById(R.id.zddl_lzh);
        dl_lzh=findViewById(R.id.dl_lzh);
    }
}